﻿Imports System.Web.UI
Imports System.Collections.Specialized
Imports System.Web



' The request context is the heart of BigfootMVC. It acts as the interface for all operations
' against the database, dnn, request and response objects, posted data, etc. 
' The reason for this isolation is to facilitate unit testing. By decoupling the code from the 
' dependencies of the intristic asp.net objects, we are able to fake up a request context object
' and be confident that all the code within the system will work using our fake context.

Namespace AboutDNN.DNNMVC

    Public Class RequestContext

        Private _contextcache As IContextCache

        Private _request As IRequest
        Private _response As IResponse
        Private _server As IServer

        Private _route As RouteInfo

        ' DNN Interface
        Private _dnn As IDNNHelper

        ' DB
        Private _db As DataProvider

        Sub New()
            _contextcache = New ASPContextCache
            _request = New ASPRequest
            _response = New ASPResponse
            _server = New ASPServer

            _route = New RouteInfo
            _route.Controller = IIf(_request.QueryString(Params.Controller) Is Nothing, "Home", _request.QueryString(Params.Controller))
            _route.Action = IIf(_request.QueryString(Params.Action) Is Nothing, "Default", _request.QueryString(Params.Action))

            ' DNN cache is application wide
            If App.Cache.Contains(Params.Cache.DNNHelper) = False Then
                App.Cache.Add(Params.Cache.DNNHelper, New DNNHelper)
            End If
            _dnn = App.Cache.GetValue(Params.Cache.DNNHelper)

            ' DB cache is application wide
            If App.Cache.Contains(Params.Cache.DB) = False Then
                App.Cache.Add(Params.Cache.DB, New DataProvider)
            End If
            _db = App.Cache.GetValue(Params.Cache.DB)
        End Sub

        Sub New(ByVal contextCache As IContextCache, _
                ByVal request As IRequest, _
                ByVal response As IResponse, _
                ByVal route As RouteInfo, _
                ByVal server As IServer, _
                ByVal dnnprovider As IDNNHelper, _
                ByVal db As DataProvider)
            _contextcache = contextCache
            _request = request
            _response = response
            _route = route
            _server = server
            _dnn = dnnprovider
            _db = db
        End Sub

        Default Public Property Items(ByVal key As String) As Object
            Get
                If _contextcache.Items.ContainsKey(key) Then
                    Return _contextcache(key)
                Else
                    Return Nothing
                End If
            End Get
            Set(ByVal value As Object)
                If _contextcache.Items.ContainsKey(key) Then
                    _contextcache(key) = value
                Else
                    _contextcache.Items.Add(key, value)
                End If
            End Set
        End Property

        Public ReadOnly Property Request() As IRequest
            Get
                Return _request
            End Get
        End Property

        Public ReadOnly Property Response() As IResponse
            Get
                Return _response
            End Get
        End Property

        Public ReadOnly Property Server() As IServer
            Get
                Return _server
            End Get
        End Property

        Public ReadOnly Property ApplicationPath() As String
            Get
                Return _request.ApplicationPath
            End Get
        End Property

        Public ReadOnly Property RequestType() As String
            Get
                Return _request.RequestType
            End Get
        End Property

        Public ReadOnly Property IsAuthenticated() As Boolean
            Get
                Return _request.IsAuthenticated
            End Get
        End Property

        Public ReadOnly Property UserName() As String
            Get
                Return _request.UserName
            End Get
        End Property

        Public ReadOnly Property Headers() As NameValueCollection
            Get
                Return _request.Headers
            End Get
        End Property

        Public Function MapPath(ByVal virtualPath As String) As String
            Return _server.MapPath(virtualPath)
        End Function

        Public Function ExecutePage(ByVal currentPage As Page) As String
            ' Render the page
            Dim output = New IO.StringWriter()
            _server.Execute(currentPage, output, True)

            ' Return the rendered output
            Return output.ToString
        End Function

        Public ReadOnly Property Route() As RouteInfo
            Get
                Return _route
            End Get
        End Property

        Public ReadOnly Property DNN() As IDNNHelper
            Get
                Return _dnn
            End Get
        End Property

        Public ReadOnly Property DB() As DataProvider
            Get
                Return _db
            End Get
        End Property

        ' Gets the value of the item weather it is in the querystring or posted form
        Public ReadOnly Property RequestValue(ByVal key As String) As String
            Get
                If _request.QueryString(key) IsNot Nothing Then
                    Return _request.QueryString(key)
                ElseIf _request.Form(key) IsNot Nothing Then
                    Return _request.Form(key)
                Else
                    Return String.Empty
                End If
            End Get
        End Property

        ' A name value collection including QueryString and Form values
        Public ReadOnly Property RequestValues() As NameValueCollection
            Get
                Dim _values As New NameValueCollection
                _values.Add(_request.QueryString)
                _values.Add(_request.Form)
                Return _values
            End Get
        End Property

        Public ReadOnly Property QueryString() As NameValueCollection
            Get
                Return _request.QueryString
            End Get
        End Property

        Public ReadOnly Property Form() As NameValueCollection
            Get
                Return _request.Form
            End Get
        End Property

        Public ReadOnly Property Cookies() As HttpCookieCollection
            Get
                Return _request.Cookies
            End Get
        End Property

        Public ReadOnly Property Files() As List(Of HttpPostedFile)
            Get
                Return _request.Files
            End Get
        End Property

    End Class


End Namespace
